import { SceneFramebuffer } from './SceneFramebuffer.js'

export class PostProcess {
  constructor(options = {}) {
    this._drawingBufferSize = new THREE.Vector2();
    this._renderer.getDrawingBufferSize(this._drawingBufferSize)

    this._renderer = options.renderer;

    this._sceneFramebuffer = new SceneFramebuffer({
      width: this._drawingBufferSize.x,
      height: this._drawingBufferSize.y,
    })

    const viewportQuad = new THREE.Mesh(new THREE.PlaneBufferGeometry(2, 2), null);
    viewportQuad.frustumCulled = false;

    this._postProcessCamera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1);
    this._postProcessScene = new THREE.Scene();
    this._postProcessScene.add(viewportQuad);
  }

  render(scene, camera, autoClear = true) {
    const {
      _renderer: renderer,
      _sceneFramebuffer: sceneFramebuffer,
    } = this;

    const originalAutoClear = renderer.autoClear;
    renderer.autoClear = false;
    renderer.setRenderTarget(sceneFramebuffer.framebuffer)

    if (autoClear) renderer.clear();
    renderer.render(scene, camera);
    renderer.autoClear = originalAutoClear;

    this._resolveFramebuffers()
  }

  _resolveFramebuffers() {
    const {
      _renderer: renderer,
      _sceneFramebuffer: sceneFramebuffer,
    } = this;

    renderer.setRenderTarget(null)
    renderer
  }
}
